import api, { logout } from '@/api'
import home from "@/api/home";
import { LOGIN_USERINFO, BASE_DATA } from '@/utils/constant'
import StorageModel from '@/utils/storage'

const Storage = new StorageModel()

const initialState = {
  pageReloadRandom: null,
  loginUserinfo: Storage.get(LOGIN_USERINFO) || null,
  baseData: Storage.get(BASE_DATA) || null,
}

let that = {
  source: null
}

export default {
  namespaced: true,
  state: initialState,
  getters: {},
  actions: {
    async getBaseData ({ commit }) {
      return new Promise( async (resolve, reject) => {
        try {
          if (typeof that.source === "function") {
            that.source("终止请求"); //取消请求
          }
          let { data } = await home.getBase(that)
          if (data.code == 0) {
            commit({ type: "changeBaseData", data: data.data || null });
          }
          resolve(data.data)
        } catch (err) {
          reject(err)
        }
      })
    },
  },
  mutations: {
    changePageReloadRanDom(state, newState) {
      state.pageReloadRandom = Date.now()
    },
    changeBaseData(state, newState) {
      state.baseData = newState
      Storage.set(BASE_DATA, newState)
    },
    changeLoginUserInfo(state, newState) {
      state.loginUserinfo = newState
      Storage.set(LOGIN_USERINFO, newState)
    },
  }
}
